/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 * ClassName:MapUtil <br/>
 * Date: 2018年11月11日 下午5:31:28 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
public class MapUtil {

    /**
     * 将Map按Key进行排序，用户实现排序的Comparator
     * 
     * @param oriMap
     * @param comparator
     * @return
     */
    public static <K,V> Map<K, V> sortMapByKey(Map<K, V> oriMap, Comparator<K> comparator) {
        if (oriMap == null || oriMap.isEmpty()) {
            return oriMap;
        }

        Map<K, V> sortMap = new TreeMap<K, V>(comparator);

        sortMap.putAll(oriMap);

        return sortMap;
    }

    /**
     * 将Map按Key进行排序，Map的Key是String类型
     * 
     * @param oriMap
     * @return
     */
    public static <T> Map<String, T> sortMapByStringKey(Map<String, T> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return oriMap;
        }

        Map<String, T> sortMap = new TreeMap<String, T>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }

        });

        sortMap.putAll(oriMap);

        return sortMap;
    }

    /**
     * 使用 Map按value进行排序
     * 
     * @param map
     * @return
     */
    public static <T> Map<String, T> sortMapByValue(Map<String, T> oriMap,
                                                    Comparator<Map.Entry<String, T>> comparator) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, T> sortedMap = new LinkedHashMap<String, T>();
        List<Map.Entry<String, T>> entryList = new ArrayList<Map.Entry<String, T>>(oriMap.entrySet());
        Collections.sort(entryList, comparator);
        Iterator<Map.Entry<String, T>> iter = entryList.iterator();
        Map.Entry<String, T> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }

    /**
     * 使用 Map按value进行排序，注此处的Key只能够是整数
     * 
     * @param map
     * @return
     */
    public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap, boolean reverse) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(oriMap.entrySet());
        Comparator<Map.Entry<String, Integer>> comparator = new MapValueComparator();
        if (reverse) {
            comparator = new MapValueReverseComparator();
        }
        Collections.sort(entryList, comparator);
        Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
        Map.Entry<String, Integer> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }

    public static List<String> getTop5Keyword(Map<String, Integer> map) {
        List<String> keyword = new ArrayList<String>();
        Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
        int num = 0;
        while (it.hasNext()) {
            Entry<String, Integer> entry = it.next();
            keyword.add(entry.getKey());
            num++;
            if (num >= 5) {
                break;
            }
        }
        return keyword;
    }
}

class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {

    @Override
    public int compare(Entry<String, Integer> me1, Entry<String, Integer> me2) {

        return me1.getValue().compareTo(me2.getValue());
    }
}

class MapValueReverseComparator implements Comparator<Map.Entry<String, Integer>> {

    @Override
    public int compare(Entry<String, Integer> me1, Entry<String, Integer> me2) {

        return me2.getValue().compareTo(me1.getValue());
    }
}
